from bilibili_api import live, sync
from bilibili_api import Credential
import time
from datetime import datetime

credential = Credential(sessdata="6b1a13d7%2C1758939876%2C8951f%2A31CjCnuNCNztCftrPkFjag8k47dIuQZ-ut62XXcemFADFMuB25dQCJqC6nM4LKkTq7fpYSVlo0ZFNkVmVXcWhlcXhXS3hCOXZBYjYyVXdJZ0ZSNllfWFJ3T3VBZEVfUkxxNFJrNU4xd1BZNVAxcEVOc3JFUUNubV8tc045c0ZsaXR2NkMtVjFQZU1nIIEC",
                        bili_jct="0fe3dcb51de2f49bea0458875ba9c957",
                        buvid3="21386F8E-E6CB-6015-D6CB-63F5C751BB9751434infoc",
                        dedeuserid="3546869426817357")
room = live.LiveDanmaku(room_display_id = 22767426, credential=credential)

@room.on('DANMU_MSG')
async def on_danmaku(event):
    # 收到弹幕
    # print(event['data']['info'][1])
    pass
    """
    room_display_id: event['room_display_id']
    room_real_id: event['room_display_id']
    content : event['data']['info'][1]
    name: event['data']['info'][2][1]
    uid: event['data']['info'][2][0]
    
    medallevel: event['data']['info'][3][0] len(event['data']['info'][3]) 判断
    medalname: event['data']['info'][3][1]
    medalfromname: event['data']['info'][3][2]
    medalfromuid: event['data']['info'][3][12]
    
    ts :event['data']['info'][9]['ts']
    荣誉等级 :event['data']['info'][16][0]  len(event['data']['info'][16])
    3舰长 2提督 1总督 0粉丝 身份： event['data']['info'][0][15]['user']['medal']['guard_level']
    event['data']['info'][0][15]['user']['medal'] == None
    """
@room.on('SEND_GIFT')
async def on_gift(event):
    # 收到礼物
    # print(event)
    pass

@room.on('ONLINE_RANK_COUNT')
async def on_ONLINE_RANK(event):
    # 收到礼物
    # print(event)
    pass

@room.on('ALL')
async def on_all(event):
    # 收到礼物
    # print(event)
    try:
        if(event["data"]["cmd"] == "ONLINE_RANK_COUNT"):
            """
            event['room_real_id']
            event["data"]["data"]["online_count"]
            """
            timestamp_seconds = int(time.time())
            print("整数秒级时间戳:", timestamp_seconds,datetime.fromtimestamp(timestamp_seconds))
            pass
    except Exception:
        return
    pass

    """
    event['room_real_id']
    event['room_display_id']
    event['data']['data']['action']
    event['data']['data']['batch_combo_id']
    event['data']['data']['batch_combo_send']
        - ['action']
        - ['batch_combo_num']
        - ['gift_id']
        - ['gift_name']
        - ['gift_num']
        - ['uid']
        - ['uname']
    event['data']['data']['combo_send']
        - ['action']
        - ['combo_num']
        - ['gift_id']
        - ['gift_name']
        - ['gift_num']
        - ['uid']
        - ['uname']
    event['data']['data']['discount_price']
    event['data']['data']['giftId']
    event['data']['data']['giftName']
    event['data']['data']['medal_info']
        - ['medal_level']
        - ['medal_name']
        - ['is_lighted']
    event['data']['data']['num']
    event['data']['data']['price']
    event['data']['data']['receive_user_info']
        - ['uid']
        - ['栞栞Shiori']
    event['data']['data']['sender_uinfo']
        - ['medal']
            - ['guard_level']
            - ['level']
            - ['name']
            - ['ruid']
    event['data']['data']['wealth_level']        
    """
if __name__ == "__main__":
    sync(room.connect())
